﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace OuterParser
{
    public class DbCommandExecuter
    {
        internal string ConnectionString { get; set; }
        public DbCommandExecuter(string connectionStringName)
        {
            string connection = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
            this.ConnectionString = connection.Substring(connection.IndexOf("data source"), connection.IndexOf("App=EntityFramework") - connection.IndexOf("data source"));
        }
        public SqlDataReader ExecuteCommand(string sqlQuery)
        {
            if (sqlQuery == string.Empty)
                throw new ArgumentException("Empty command is not valid.");
            SqlCommand myCommand = new SqlCommand(sqlQuery);

            using (SqlConnection con = new SqlConnection(this.ConnectionString))
            {
                // establishing the connection
                con.Open();
                myCommand.Connection = con;

                // sending the request and then filling the table.
                return myCommand.ExecuteReader();
            }

            return null;
        }
    }
}
